<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="身份证号" prop="identity">
        <el-input
          v-model="queryParams.identity"
          placeholder="请输入身份证号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="carList" v-show="rentCarTable">
      <!--      <el-table-column label="车辆id" align="center" prop="id"/>-->
      <el-table-column label="车牌号" align="center" prop="carNumber"/>
      <el-table-column label="车架号" align="center" prop="frameNumber"/>
      <el-table-column label="品牌" align="center" prop="brand"/>
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.car_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="颜色" align="center" prop="color"/>
      <el-table-column label="出租价格" align="center" prop="rentprice"/>
      <el-table-column label="出租押金" align="center" prop="deposit"/>
      <el-table-column label="描述" align="center" prop="description"/>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-truck"
            @click="toRentCar(scope.row)"
          >出租
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="showCarImgs(scope.row)"
          >查看大图
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="rentCarTable"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车辆管理对话框    租车-还车时间（可选）  出租单号（realOnly） 车牌号（realOnly） 押金（realOnly）  租金  操作员   -->
    <el-dialog title="汽车出租" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="租车时间">

          <el-date-picker type="datetime"
                          v-model="form.beginDate"
                          placeholder="请选择起租时间"
                          size="small"
                          style="width: 200px"
                          value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
          --
          <el-date-picker type="datetime"
                          v-model="form.returnDate"
                          placeholder="请选择还车时间"
                          size="small"
                          style="width: 200px"
                          value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
        </el-form-item>

        <el-form-item label="用户身份证" prop="identity">
          <el-input v-model="form.identity" readonly/>
        </el-form-item>

        <el-form-item label="出租单号" prop="rentSn">
          <el-input v-model="form.rentSn" readonly/>
        </el-form-item>

        <el-form-item label="车牌号" prop="carNumber">
          <el-input v-model="form.carNumber" readonly/>
        </el-form-item>

        <el-form-item label="出租价格" prop="rentprice">
          <el-input v-model="form.rentprice" placeholder="请输入出租价格"/>
        </el-form-item>
        <el-form-item label="出租押金" prop="deposit">
          <el-input v-model="form.deposit" readonly/>
        </el-form-item>

        <el-form-item label="操作员" prop="opername">
          <el-input v-model="form.opername" readonly/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="" :visible.sync="isShowImg" width="1200px" append-to-body>
      <!--  走马灯-->
      <span style="text-align: center">展示图</span>
      <el-carousel :interval="4000" type="card" height="500px">
        <el-carousel-item v-for="carImg in carImgs" :key="carImg">
          <img :src="carImg" alt="汽车图片" style="width: 100%;height: 400px"/>
        </el-carousel-item>
      </el-carousel>
    </el-dialog>

  </div>
</template>

<script>
import {listCar} from "@/api/business/car";
import {checkIdentity, showCarImgs, toRentCar,doRentCar} from "@/api/business/rentCar";

export default {
  name: "RentCar",
  dicts: ['car_type'],
  data() {
    return {
      carImgs: [],
      rentCarTable: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆管理表格数据
      carList: [],
      // 是否显示弹出层
      open: false,
      isShowImg: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        identity: null,
        status: 0
      },
      // 表单参数
      form: {}

    };
  },
  created() {

  },
  methods: {
    /** 查询车辆管理列表 */
    getList() {
      this.loading = true;
      listCar(this.queryParams).then(response => {
        this.carList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        carNumber: null,
        rentprice: null,
        deposit: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      // 身份证传进来  校验这个客户是否存在
      checkIdentity({identity: this.queryParams.identity}).then(res => {
        // 查询未出租的车辆
        this.getList();
        this.rentCarTable = true;
      })
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.rentCarTable = false;
    },

    /** 提交按钮 */
    submitForm() {
      doRentCar(this.form).then(res => {
        this.$modal.msgSuccess(res.msg);
        this.queryParams.identity = null;
        this.rentCarTable = false;
        this.open = false;
      });
    },

    toRentCar(row) {
      // 去租车 需要展示弹窗
      //  把汽车id发请求到后端去  生成一些数据
      toRentCar().then(res => {
        // 返回一个全局的租车单号
        this.form = {
          carNumber: row.carNumber,
          rentprice: row.rentprice,
          deposit: row.deposit,
          identity: this.queryParams.identity,
          rentSn: res.data.rentSn,
          opername: res.data.opername
        }
        // 渲染表单
        this.open = true;
      })
    },

    showCarImgs(row) {
      console.log(row)
      // 发个请求到后台把图片集合查出来
      showCarImgs({carNumber: row.carNumber}).then(res => {
        this.carImgs = res.data;
        this.isShowImg = true;
      })
    }
  }
};
</script>
